package com.teamwork;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class tkzs {
    private static final String DB_URL = "jdbc:mysql://localhost:3306/studentexam";
    private static final String USER = "root";
    private static final String PASS = "123456";

    public static void displayExamchoiceData() {
        try {
            // 1. Register JDBC driver
            Class.forName("com.mysql.cj.jdbc.Driver");

            // 2. Open a connection
            Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);

            // 3. Execute a query
            Statement stmt = conn.createStatement();
            String sql = "SELECT * FROM examchoice";
            ResultSet rs = stmt.executeQuery(sql);

            // 4. Extract data from result set
            while (rs.next()) {
                int id = rs.getInt("CHId");
                int subjectId = rs.getInt("Cno");
                String question = rs.getString("item");
                String optionA = rs.getString("CHA");
                String optionB = rs.getString("CHB");
                String optionC = rs.getString("CHC");
                String optionD = rs.getString("CHD");
                String correctAnswer = rs.getString("CHKey");

                // Display the data
                System.out.println("ID: " + id + ", Subject ID: " + subjectId + ", Question: " + question);
                System.out.println("A: " + optionA + ", B: " + optionB + ", C: " + optionC + ", D: " + optionD);
                System.out.println("Correct Answer: " + correctAnswer);
                System.out.println("-----------------------------");
            }

            // 5. Clean-up environment
            rs.close();
            stmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

